package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.GoodsMaterialMapper;
import com.gxa.zhonglong.pojo.*;
import com.gxa.zhonglong.service.AuditService;
import com.gxa.zhonglong.service.GoodsMaterialService;
import com.gxa.zhonglong.service.GoodsService;
import com.gxa.zhonglong.service.StoreBarterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author asus
 */

@CrossOrigin
@RestController
@Api(value = "商品拆料")
@RequestMapping("/GoodsMaterial")
public class GoodsMaterialController {

    @Autowired
    StoreBarterService storeBarterService = null;

    @Autowired
    AuditService auditService = null;

    @Autowired
    GoodsService goodsService = null;

    @Autowired
    GoodsMaterialService goodsMaterialService = null;

    @PostMapping("/putMaterialGoods")
    @ApiOperation(value ="添加商品拆料" )
    public Map insertGoodsMaterial(@RequestBody GoodsMaterialAndAuditVo goodsMaterialAndAuditVo) {
        Audit audit = goodsMaterialAndAuditVo.getAudit();
        GoodsMaterial goodsMaterial = goodsMaterialAndAuditVo.getGoodsMaterial();
        goodsMaterialService.insertGoodsMaterial(goodsMaterial);
        auditService.insertAudit(audit);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getStore")
    @ApiOperation(value = "查询store(出库门店)表的数据")
    public Map selectSupplierAndStore() throws Exception {
        //获取门店信息
        List<String> store = storeBarterService.selectAllStore();

        Map<String,List> map = new HashMap<>();

        map.put("store",store);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(map);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/getCounter")
    @ApiOperation(value = "根据门店查询柜台")
    public Map selectCounter(@RequestParam("barterName")  String barterName) throws Exception {

        List<String> counter = storeBarterService.selectCounter(barterName);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(counter);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }




    @PostMapping("/getGoodsMaterial")
    @ApiOperation(value = "根据条件多表查询")
    public Map selectBY(@RequestBody Where where ){
        System.out.println(where);
        List<Map<String, Object>> selectByWhere = goodsMaterialService.selectByWhere(where);
        System.out.println(selectByWhere);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("selectByWhere",selectByWhere);
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteByReceiptNumber")
    @ApiOperation(value = "根据商品编码删除商品记录")
    public Map deleteByGoodsMaterialNumber(@RequestParam("number") String number) throws BusinessException {
        goodsService.deleteByNumber(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/setByReceiptNumber")
    @ApiOperation(value = "根据单据编号更新商品拆料单记录")
    public Map UpdateByGoodsMaterialNumber(@RequestBody GoodsMaterial goodsMaterial){
        goodsMaterialService.updateByReceiptNumber(goodsMaterial);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getByReceiptNumber")
    @ApiOperation(value = "根据单据编号查看商品表的记录")
    public Map selectByReceiptNumber(@RequestParam("receiptNumber") String receiptNumber){
        List<Map<String, Object>> maps = goodsMaterialService.selectByReceiptNumberGoods(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getByReceiptNumberFixedCodingDetail")
    @ApiOperation(value = "根据单据编号查看拆料表的明细")
    public Map getByReceiptNumberFixedCoding(@RequestParam("receiptNumber") String receiptNumber){
        List<Map<String, Object>> maps = goodsMaterialService.selectByReceiptNumberGoodsMaterial(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }


    @PostMapping("/deleteByFixedCoding")
    @ApiOperation(value = "根据单据编号删除固定编码入库表的记录")
    public Map deleteByInventory(@RequestParam("receiptNumber") String receiptNumber){

        goodsMaterialService.deleteByReceiptNumberGoodsMaterial(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/setAudit")
    @ApiOperation(value = "根据单据编号更新审批表的记录")
    public Map updateByAudit(@RequestBody Audit audit) throws BusinessException {

        auditService.updateByReceiptNumberAudit(audit);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/putFixedCodingGoods")
    @ApiOperation(value = "根据商品编号添加拆料表明细")
    public Map updateByAudit(@RequestBody Map map){

        String receiptNumber =(String) map.get("receiptNumber");
        String number =(String) map.get("number");
        Goods goods = goodsService.selectGoods(number);
        goods.setReceiptNumber(receiptNumber);
        goodsMaterialService.insertGoodsMaterialD(goods);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteByGoodsNumber")
    @ApiOperation(value = "根据商品编码删除商品记录")
    public Map deleteByGoodsNumber(@RequestParam("number") String number ) throws BusinessException {
        goodsMaterialService.deleteByNumber(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


}

